from selenium.webdriver.common.by import By
from pages.base_page import Page


class PostChatPage(Page):
    ele_title = (By.ID, u'page-postChat-title')
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    ele_feature_switch = (By.XPATH, u"//span[@data-tag='featureSwitch']")

    ele_greeting_message = (By.XPATH, u"//p[@data-tag='cardTitle' and text()='Greeting Message']")
    ele_fields = (By.XPATH, u"//p[text()='Fields']")

    ele_is_enable_feature_switch = (By.XPATH, u"//span[@data-tag='featureSwitch']/span/div")

    ele_field_layout = (By.XPATH, u"//p[@data-tag='cardTitle' and text()='Field Layout']")
    ele_field_layout_left = (By.XPATH, u"//input[@name='formFieldLayoutStyle' and @value='leftofInput']")
    ele_field_layout_above = (By.XPATH, u"//input[@name='formFieldLayoutStyle' and @value='aboveInput']")

    ele_postchat_save_button = (By.XPATH, u"(//button[@data-tag='formButton'])[1]")

    # 如果没有post chat功能，则检查图片和unlock button
    ele_title_no_post_feature = (By.ID, u'PostChat-title')
    ele_img = (By.XPATH, u'//img[@alt="Post Chat Introduction"]')
    ele_unlock_this_feature = (By.XPATH, u'//span[text()="Unlock This Feature"]')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    """
    #the two methods moved to base class
    def goto_page(self):
        self.driver.get(self.url)

    def check_page_has_error(self):
        try:
            flag = self.find_element(*self.ele_error).is_enabled()
            if flag:
                raise Exception("current page {} may has error".format(self.url))
        except Exception as e:
            return True
    """

    # this method should be called first
    def is_enable_feature_switch(self):
        fs = self.find_element(*self.ele_is_enable_feature_switch)
        if fs.text == 'OFF':
            self.click(self.ele_feature_switch)
        else:
            pass

    def post_chat_page_eles_check_when_has_post_chat_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_feature_switch).is_enabled()
        self.find_element(*self.ele_greeting_message).is_enabled()
        self.find_element(*self.ele_fields).is_enabled()
        self.find_element(*self.ele_field_layout_left).is_enabled()
        self.find_element(*self.ele_field_layout_above).is_enabled()
        self.find_element(*self.ele_postchat_save_button).is_enabled()

    def post_chat_page_eles_check_when_no_post_chat_feature(self):
        """
        没有post chat这个feature时，在测试用例中调用这个方法
        :return:
        """
        self.find_element(*self.ele_title_no_post_feature).is_enabled()
        self.find_element(*self.ele_img).is_enabled()
        self.find_element(*self.ele_unlock_this_feature).is_enabled()

    def post_chat_button_save(self):
        self.click(self.ele_field_layout)
        self.click(self.ele_field_layout_left)
        self.click(self.ele_field_layout_above)
        self.click(self.ele_postchat_save_button)
